// 一. 导入express包
const express = require('express')

// 二. 实例化路由对象
const router = express.Router()
const { getAll, getById, exec } = require('../db')
// 三. 编写接口

/**
 * 获取用户列表
 * GET /
 */
router.get('/videoList', async (req, res) => {
  // 解析参数
  try {
    const {
      page = 1,
      size = 10,
      sortOrder = 'asc',
      sort = 'video_id',
    } = req.query
    // 分页的公式: 偏移量 = (当前页码数 - 1)*每页显示数ORDER BY Company DESC
    let offset = (page - 1) * size
    // 操作数据库
    let sql2 = `select count(*) as total from gg_video`
    let { total } = await exec(sql2) // { total: 10 }
    var sql = `select * from gg_video order by ${sort} ${sortOrder} limit ${offset}, ${size} `

    const data = await getAll(sql)

    // 返回数据
    res.send({
      code: 0,
      message: '获取list成功',
      result: {
        total: total,
        rows: data,
      },
    })
  } catch (err) {
    // 异常的情况
    res.send({
      code: 100101,
      message: '获取失败',
      result: err,
    })
  }
})

/**
 * 获取用户列表
 * GET /
 */
router.get('/all', async (req, res) => {
  try {
    let sql = `select * from gg_video`
    let data = await getAll(sql)

    // 返回数据
    res.send({
      code: 0,
      message: '获取全部监控成功',
      result: {
        data: data,
      },
    })
  } catch (err) {
    // 异常的情况
    res.send({
      code: 100101,
      message: '获取失败',
      result: err,
    })
  }
})

// 四. 导出router对象
module.exports = router
